package com.neolix.watch.view;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


import com.neolix.watch.R;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static android.view.animation.AnimationUtils.loadAnimation;

public class ConfirmDialog extends Dialog {

    private TextView titleView;
    protected Button positiveButton;
    protected Button negativeButton;
    private TextView messageView;
    protected ImageView waitingSpinner;
    private boolean isProgressTipVisible;

    public ConfirmDialog(Context context) {
        this(context, true);
    }

    public ConfirmDialog(Context context, boolean isProgressTipVisible) {
        super(context, android.R.style.Theme_Translucent_NoTitleBar);
        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        setContentView(R.layout.confirm_dialog_view);

        initUi();
        initListener();

        this.isProgressTipVisible = isProgressTipVisible;
    }

    @Override
    public void show() {
        super.show();
        if (isProgressTipVisible) startAnimation();
    }

    private void initListener() {
        positiveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        negativeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }

    private void initUi() {
        titleView = (TextView) findViewById(R.id.title);
        messageView = (TextView) findViewById(R.id.message);
        positiveButton = (Button) findViewById(R.id.positive_button);
        negativeButton = (Button) findViewById(R.id.negative_button);
        waitingSpinner = (ImageView) findViewById(R.id.waiting_spinner);
    }

    public void setTitle(String title) {
        titleView.setText(title);
        titleView.setVisibility(VISIBLE);
    }

    public void setPositiveButton(String text, View.OnClickListener onClickListener) {
        positiveButton.setText(text);
        positiveButton.setOnClickListener(onClickListener);
    }

    public void setNegativeButton(String text, View.OnClickListener onClickListener) {
        negativeButton.setText(text);
        negativeButton.setOnClickListener(onClickListener);
    }

    public void setMessage(CharSequence text) {
        isProgressTipVisible = false;
        titleView.setVisibility(GONE);
        messageView.setText(text);
        messageView.setTextSize(18);
        messageView.setVisibility(VISIBLE);
        waitingSpinner.setVisibility(GONE);

        stopAnimation();
    }

    public void stopAnimation() {
        waitingSpinner.clearAnimation();
    }

    public void startAnimation() {
        waitingSpinner.setVisibility(VISIBLE);

        Animation spinAnimation = loadAnimation(getContext(), R.anim.loading_indicator);
        waitingSpinner.startAnimation(spinAnimation);
    }

    public void setPositiveButtonVisible(int visible) {
        positiveButton.setVisibility(visible);
    }

    public void displayWithStyle(DisplayStyle style) {
        negativeButton.setVisibility(style.negativeVisibility);
        positiveButton.setVisibility(style.positiveVisibility);
    }

    public void setNegativeButtonVisible(int visible) {
        negativeButton.setVisibility(visible);
    }

    public enum DisplayStyle {
        POSITIVE(VISIBLE, GONE),
        NEGATIVE(GONE, VISIBLE),
        POSITIVE_AND_NEGATIVE(VISIBLE, VISIBLE);

        public int positiveVisibility = VISIBLE;

        public int negativeVisibility = VISIBLE;

        DisplayStyle(int positiveVisibility, int negativeVisibility) {
            this.positiveVisibility = positiveVisibility;
            this.negativeVisibility = negativeVisibility;
        }
    }
}